package com.controller;

import com.dto.Result;
import com.entity.EngineerProjectInfo;
import com.service.EngineerProjectInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/engineer/project/info")
public class EngineerProjectInfoController {

    @Autowired
    private EngineerProjectInfoService engineerProjectInfoService;

    @PostMapping
    public EngineerProjectInfo create(@RequestBody EngineerProjectInfo engineerProjectInfo) {
        return engineerProjectInfoService.save(engineerProjectInfo);
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable Integer id) {
        engineerProjectInfoService.deleteById(id);
    }

    @PutMapping("/{id}")
    public EngineerProjectInfo update(@PathVariable Integer id, @RequestBody EngineerProjectInfo engineerProjectInfo) {
        return engineerProjectInfoService.update(id, engineerProjectInfo);
    }

    @GetMapping("/{id}")
    public EngineerProjectInfo detail(@PathVariable Integer id) {
        return engineerProjectInfoService.findById(id).orElse(null);
    }

    @GetMapping("/list")
    public List<EngineerProjectInfo> list() {
        return engineerProjectInfoService.findAll();
    }

    @GetMapping("/page")
    public Page<EngineerProjectInfo> page(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        return engineerProjectInfoService.findAll(pageable);
    }
}